<template>
	<view class="center">
		<view class="userInfo" @click.capture="toUserInfo">
			<cloud-image width="150rpx" height="150rpx" v-if="hasLogin&&userInfo.avatar_file&&userInfo.avatar_file.url"
				:src="userInfo.avatar_file.url"></cloud-image>

			<view v-else class="defaultAvatarUrl">
				<uni-icons color="#ffffff" size="50" type="person-filled" />
			</view>

			<view class="logo-title">
				<text class="uer-name" v-if="hasLogin">{{userInfo.nickname||userInfo.username||userInfo.mobile}}</text>
				<text class="uer-name" v-else>{{'登录'}}</text>
			</view>
		</view>
		<uni-grid class="grid" :column="2" :showBorder="false" :square="true">
			<uni-grid-item class="item" v-for="(item,index) in gridList" :key="index">
				<!-- <uni-icons class="icon" color="#007AFF" :type="item.icon" size="26"></uni-icons> -->
				<text class="text" style="font-size: 30px;font-weight: 600;">{{item.num}}</text>
				<text class="text">{{item.text}}</text>
			</uni-grid-item>
		</uni-grid>

		<uni-list class="center-list" v-for="(sublist , index) in ucenterList" :key="index">
			<uni-list-item v-for="(item,i) in sublist" :title="item.title" link :rightText="item.rightText" :key="i"
				:clickable="true" :to="item.to" :show-extra-icon="true" :extraIcon="{type:item.icon,color:'#999'}">
				<template v-slot:footer>
					<view v-if="item.showBadge" class="item-footer">
						<text class="item-footer-text">{{item.rightText}}</text>
						<view class="item-footer-badge"></view>
					</view>
				</template>
			</uni-list-item>
		</uni-list>
	</view>
</template>

<script>
	const db = uniCloud.database();
	const activity_db = db.collection('abu_baoming');
	const user_db = db.collection('abu_user_info');
	import {
		store,
		mutations
	} from '@/uni_modules/uni-id-pages/common/store.js'
	export default {
		data() {
			return {
				gridList: [{
						"num": 0,
						"text": '报名次数'
					},
					{
						"num": 0,
						"text": "参与次数"
					}
				],
				ucenterList: [
					[{
						"title": "个人信息",
						"to": '',
						"icon": "home"
					}, {
						"title": "意见反馈",
						"to": '/uni_modules/uni-feedback/pages/opendb-feedback/opendb-feedback',
						"icon": "help"
					}, {
						"title": "设置",
						"to": '/pages/ucenter/settings/settings',
						"icon": "gear"
					}]

				],
				listStyles: {
					"height": "150rpx", // 边框高度
					"width": "150rpx", // 边框宽度
					"border": { // 如果为 Boolean 值，可以控制边框显示与否
						"color": "#eee", // 边框颜色
						"width": "1px", // 边框宽度
						"style": "solid", // 边框样式
						"radius": "100%" // 边框圆角，支持百分比
					}
				}
			}
		},
		computed: {
			userInfo() {
				return store.userInfo
			},
			hasLogin() {
				return store.hasLogin
			},
			appConfig() {
				return getApp().globalData.config
			}
		},
		async onLoad() {

			await this.set_user_path()

		},
		onShow() {
			this.gridList[0].num = 0;
			this.gridList[1].num = 0;
			this.load_activity_data();
		},
		methods: {
			async set_user_path() {
				const user_info = await uniCloud.getCurrentUserInfo()
				console.log(user_info.uid);
				const res_d = await user_db.where({
					user_id: user_info.uid
				}).get()

				if (res_d.result && res_d.result.data && res_d.result.data.length == 0) {
					console.log(res_d.result);
					this.ucenterList[0][0].to = "/uni_modules/abu_user_info/pages/abu_user_info/add"
				} else {
					this.ucenterList[0][0].to = "/uni_modules/abu_user_info/pages/abu_user_info/edit?id=" + res_d
						.result
						.data[0]._id
				}
			},
			async load_activity_data() {
				const user_info = await uniCloud.getCurrentUserInfo()
				const res_data = await activity_db.where({
					user_id: user_info.uid
				}).get();

				res_data.result.data.map(item => {
					console.log(item);
					if (item.RegistrationStatus == 1) {
						this.gridList[0].num++
					}
					if (item.RegistrationStatus == 0) {
						this.gridList[1].num++
					}
				})
			},
			toSettings() {
				uni.navigateTo({
					url: "/pages/ucenter/settings/settings"
				})
			},
			toUserInfo() {
				uni.navigateTo({
					url: '/uni_modules/uni-id-pages/pages/userinfo/userinfo'
				})
			},
			async changeLoginState() {
				if (this.hasLogin) {
					await mutations.logout()
				} else {
					uni.redirectTo({
						url: '/uni_modules/uni-id-pages/pages/login/login-withoutpwd',
					});
				}
			}



		}
	}
</script>

<style lang="scss" scoped>
	/* #ifndef APP-NVUE */
	view {
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}

	page {
		background-color: #f8f8f8;
	}

	/* #endif*/

	.center {
		flex: 1;
		flex-direction: column;
		background-color: #f8f8f8;
	}

	.userInfo {
		// padding: 20rpx;
		padding-top: 60px;
		background-image: url(../../static/uni-center/headers.png);
		flex-direction: column;
		align-items: center;
	}

	.defaultAvatarUrl {
		width: 150rpx;
		height: 150rpx;
		background-color: #007aff;
		border-radius: 100%;
		justify-content: center;
		align-items: center;
	}

	.logo-title {
		flex: 1;
		align-items: center;
		justify-content: space-between;
		flex-direction: row;
	}

	.uer-name {
		height: 100rpx;
		line-height: 100rpx;
		font-size: 38rpx;
		color: #FFFFFF;
	}

	.center-list {
		margin-bottom: 30rpx;
		background-color: #f9f9f9;
	}

	.center-list-cell {
		width: 750rpx;
		background-color: #007AFF;
		height: 40rpx;
	}

	.grid {
		background-color: #FFFFFF;
		margin-bottom: 6px;
	}

	.uni-grid .text {
		font-size: 16px;
		height: 25px;
		line-height: 25px;
		color: #817f82;
	}

	.uni-grid .item ::v-deep .uni-grid-item__box {
		justify-content: center;
		align-items: center;
	}


	/*修改边线粗细示例*/
	/* #ifndef APP-NVUE */
	.center-list ::v-deep .uni-list--border:after {
		-webkit-transform: scaleY(0.2);
		transform: scaleY(0.2);
		margin-left: 80rpx;
	}

	.center-list ::v-deep .uni-list--border-top,
	.center-list ::v-deep .uni-list--border-bottom {
		display: none;
	}

	/* #endif */
	.item-footer {
		flex-direction: row;
		align-items: center;
	}

	.item-footer-text {
		color: #999;
		font-size: 24rpx;
		padding-right: 10rpx;
	}

	.item-footer-badge {
		width: 20rpx;
		height: 20rpx;
		/* #ifndef APP-NVUE */
		border-radius: 50%;
		/* #endif */
		/* #ifdef APP-NVUE */
		border-radius: 10rpx;
		/* #endif */
		background-color: #DD524D;
	}
</style>